﻿using System;
using System.Linq;
using System.Xml.Linq;
using NUnit.Framework;
using StrategicDataSystems.Cloud.Monitoring.HostedServices;
using StrategicDataSystems.Cloud.Monitoring.HostedServices.ResponseToEntityMappers;

namespace StrategicDataSystems.Cloud.Monitoring.Tests.Given_A_HostedServicePropertiesResponseMapper
{
    [TestFixture]
    public class When_ConvertToEntity_Is_Called
    {
        [Test]
        public void Should_Return_A_Populated_HostedService_With_No_Deployments()
        {
            XDocument testResponse = XDocument.Parse(
               @"<HostedService xmlns='http://schemas.microsoft.com/windowsazure' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
                      <Url>https://management.core.windows.net/DDB588AB-F97D-4014-85EF-6FC476341FCF/services/hostedservices/service1</Url>
                      <ServiceName>service1</ServiceName>
                      <HostedServiceProperties>
                        <Description />
                        <AffinityGroup>2ae75ce7-4f6a-477d-8025-f107d3503072</AffinityGroup>
                        <Label>QmluYXJ5SGVsaXg=</Label>
                      </HostedServiceProperties>
                    </HostedService>"
               );

            HostedServicePropertiesResponseMapper mapper = new HostedServicePropertiesResponseMapper();
            HostedService populatedService = mapper.MapToEntity(testResponse);

            Assert.AreEqual("service1", populatedService.ServiceName);
            Assert.AreEqual(new Uri("https://management.core.windows.net/DDB588AB-F97D-4014-85EF-6FC476341FCF/services/hostedservices/service1"),
                populatedService.ManagementUrl);
        }

        [Test]
        public void Should_Return_A_Populated_HostedService_With_A_Deployment()
        {
            XDocument testResponse = XDocument.Parse(
                @"<HostedService xmlns='http://schemas.microsoft.com/windowsazure' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
                      <Url>https://management.core.windows.net/DDB588AB-F97D-4014-85EF-6FC476341FCF/services/hostedservices/service1</Url>
                      <ServiceName>service1</ServiceName>
                      <HostedServiceProperties>
                        <Description />
                        <AffinityGroup>2de75ce7-4f6a-477d-8025-f107d3503072</AffinityGroup>
                        <Label>TWlrZVdv</Label>
                      </HostedServiceProperties>
                      <Deployments>
                        <Deployment>
                          <Name>2684f87659484e21bff2aa50db6735d7</Name>
                          <DeploymentSlot>Production</DeploymentSlot>
                          <PrivateID>d587cb46157f47909474328894933718</PrivateID>
                          <Status>Running</Status>
                          <Label>SGVsbG9Xb3JsZCAtIDYvMTIvMjAxMCA0OjQwOjEwIFBN</Label>
                          <Url>http://service1.cloudapp.net/</Url>
                          <Configuration>PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTE2Ij8+DQo8U2VydmljZUNvbmZpZ3VyYXRpb24geG1sbnM6eHNpPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYS1pbnN0YW5jZSIgeG1sbnM6eHNkPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYSIgc2VydmljZU5hbWU9IiIgb3NWZXJzaW9uPSIqIiB4bWxucz0iaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS9TZXJ2aWNlSG9zdGluZy8yMDA4LzEwL1NlcnZpY2VDb25maWd1cmF0aW9uIj4NCiAgPFJvbGUgbmFtZT0iSGVsbG9Xb3JkX1dlYiI+DQogICAgPENvbmZpZ3VyYXRpb25TZXR0aW5ncz4NCiAgICAgIDxTZXR0aW5nIG5hbWU9IkRpYWdub3N0aWNzQ29ubmVjdGlvblN0cmluZyIgdmFsdWU9IlVzZURldmVsb3BtZW50U3RvcmFnZT10cnVlIiAvPg0KICAgIDwvQ29uZmlndXJhdGlvblNldHRpbmdzPg0KICAgIDxJbnN0YW5jZXMgY291bnQ9IjEiIC8+DQogICAgPENlcnRpZmljYXRlcyAvPg0KICA8L1JvbGU+DQo8L1NlcnZpY2VDb25maWd1cmF0aW9uPg==</Configuration>
                          <RoleInstanceList>
                            <RoleInstance>
                              <RoleName>HelloWord_Web</RoleName>
                              <InstanceName>HelloWord_Web_IN_0</InstanceName>
                              <InstanceStatus>Busy</InstanceStatus>
                            </RoleInstance>
                          </RoleInstanceList>
                          <UpgradeDomainCount>1</UpgradeDomainCount>
                          <RoleList>
                            <Role>
                              <RoleName>HelloWord_Web</RoleName>
                              <OsVersion>WA-GUEST-OS-1.4_201005-01</OsVersion>
                            </Role>
                          </RoleList>
                        </Deployment>
                      </Deployments>
                    </HostedService>");

            HostedServicePropertiesResponseMapper mapper = new HostedServicePropertiesResponseMapper();
            HostedService populatedService = mapper.MapToEntity(testResponse);

            Assert.AreEqual(1, populatedService.Deployments.Count());
            Deployment populatedDeployment = populatedService.Deployments.First();
            Assert.AreEqual("Production", populatedDeployment.Slot);
            Assert.AreEqual("Running", populatedDeployment.Status);
            Assert.AreEqual("HelloWorld - 6/12/2010 4:40:10 PM", populatedDeployment.Label);
            Assert.AreEqual("service1", populatedDeployment.HostedServiceName);
            Assert.AreEqual("http://service1.cloudapp.net/", populatedDeployment.Url.ToString());
        }

        [Test]
        public void Should_Return_A_Populated_HostedService_With_Two_Deployments()
        {

            XDocument testResponse = XDocument.Parse(
                @"<HostedService xmlns='http://schemas.microsoft.com/windowsazure' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
                      <Url>https://management.core.windows.net/DDB588AB-F97D-4014-85EF-6FC476341FCF/services/hostedservices/service1</Url>
                      <ServiceName>service1</ServiceName>
                      <HostedServiceProperties>
                        <Description />
                        <AffinityGroup>2de75ce7-4f6a-477d-8025-f107d3503072</AffinityGroup>
                        <Label>TWlrZVdv</Label>
                      </HostedServiceProperties>
                      <Deployments>
                        <Deployment>
                          <Name>2684f87659484e21bff2aa50db6735d7</Name>
                          <DeploymentSlot>Production</DeploymentSlot>
                          <PrivateID>d587cb46157f47909474328894933718</PrivateID>
                          <Status>Running</Status>
                          <Label>SGVsbG9Xb3JsZCAtIDYvMTIvMjAxMCA0OjQwOjEwIFBN</Label>
                          <Url>http://service1.cloudapp.net/</Url>
                          <Configuration>PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTE2Ij8+DQo8U2VydmljZUNvbmZpZ3VyYXRpb24geG1sbnM6eHNpPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYS1pbnN0YW5jZSIgeG1sbnM6eHNkPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYSIgc2VydmljZU5hbWU9IiIgb3NWZXJzaW9uPSIqIiB4bWxucz0iaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS9TZXJ2aWNlSG9zdGluZy8yMDA4LzEwL1NlcnZpY2VDb25maWd1cmF0aW9uIj4NCiAgPFJvbGUgbmFtZT0iSGVsbG9Xb3JkX1dlYiI+DQogICAgPENvbmZpZ3VyYXRpb25TZXR0aW5ncz4NCiAgICAgIDxTZXR0aW5nIG5hbWU9IkRpYWdub3N0aWNzQ29ubmVjdGlvblN0cmluZyIgdmFsdWU9IlVzZURldmVsb3BtZW50U3RvcmFnZT10cnVlIiAvPg0KICAgIDwvQ29uZmlndXJhdGlvblNldHRpbmdzPg0KICAgIDxJbnN0YW5jZXMgY291bnQ9IjEiIC8+DQogICAgPENlcnRpZmljYXRlcyAvPg0KICA8L1JvbGU+DQo8L1NlcnZpY2VDb25maWd1cmF0aW9uPg==</Configuration>
                          <RoleInstanceList>
                            <RoleInstance>
                              <RoleName>HelloWord_Web</RoleName>
                              <InstanceName>HelloWord_Web_IN_0</InstanceName>
                              <InstanceStatus>Ready</InstanceStatus>
                            </RoleInstance>
                          </RoleInstanceList>
                          <UpgradeDomainCount>1</UpgradeDomainCount>
                          <RoleList>
                            <Role>
                              <RoleName>HelloWord_Web</RoleName>
                              <OsVersion>WA-GUEST-OS-1.4_201005-01</OsVersion>
                            </Role>
                          </RoleList>
                        </Deployment>
                        <Deployment>
                          <Name>e1b9fc06-8363-4aab-896c-72dad8d94584</Name>
                          <DeploymentSlot>Staging</DeploymentSlot>
                          <PrivateID>9a5c4be0a3224e58a98851bc15568706</PrivateID>
                          <Status>Suspended</Status>
                          <Label>djEuMA==</Label>
                          <Url>http://9a5c4be0a3224e58a98851bc15568706.cloudapp.net/</Url>
                          <Configuration>PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTE2Ij8+DQo8U2VydmljZUNvbmZpZ3VyYXRpb24geG1sbnM6eHNpPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYS1pbnN0YW5jZSIgeG1sbnM6eHNkPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYSIgc2VydmljZU5hbWU9IiIgb3NWZXJzaW9uPSJXQS1HVUVTVC1PUy0xLjNfMjAxMDA0LTAxIiB4bWxucz0iaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS9TZXJ2aWNlSG9zdGluZy8yMDA4LzEwL1NlcnZpY2VDb25maWd1cmF0aW9uIj4NCiAgPFJvbGUgbmFtZT0iVGVzdDEiPg0KICAgIDxDb25maWd1cmF0aW9uU2V0dGluZ3M+DQogICAgICA8U2V0dGluZyBuYW1lPSJEaWFnbm9zdGljc0Nvbm5lY3Rpb25TdHJpbmciIHZhbHVlPSJEZWZhdWx0RW5kcG9pbnRzUHJvdG9jb2w9aHR0cHM7QWNjb3VudE5hbWU9YWlhdGVzdHN0b3JhZ2U7QWNjb3VudEtleT1OeVk2YXRHUmlxQUk5S2NCTHlUNnl1cmVqQUNCM0xJZnkzQzRkMmZJdnAxL3pPVi9SWVp4UktYQXp4dzlqZTdJOHBLdFRvQzZXQWNMRUhJdzMvTFo3QT09IiAvPg0KICAgIDwvQ29uZmlndXJhdGlvblNldHRpbmdzPg0KICAgIDxJbnN0YW5jZXMgY291bnQ9IjIiIC8+DQogICAgPENlcnRpZmljYXRlcyAvPg0KICA8L1JvbGU+DQo8L1NlcnZpY2VDb25maWd1cmF0aW9uPg==</Configuration>
                          <RoleInstanceList>
                            <RoleInstance>
                              <RoleName>Test1</RoleName>
                              <InstanceName>Test1_IN_0</InstanceName>
                              <InstanceStatus>Stopped</InstanceStatus>
                            </RoleInstance>
                            <RoleInstance>
                              <RoleName>Test1</RoleName>
                              <InstanceName>Test1_IN_1</InstanceName>
                              <InstanceStatus>Stopped</InstanceStatus>
                            </RoleInstance>
                          </RoleInstanceList>
                          <UpgradeDomainCount>2</UpgradeDomainCount>
                          <RoleList>
                            <Role>
                              <RoleName>Test1</RoleName>
                              <OsVersion>WA-GUEST-OS-1.3_201004-01</OsVersion>
                            </Role>
                          </RoleList>
                        </Deployment>
                      </Deployments>
                    </HostedService>"
                );

            HostedServicePropertiesResponseMapper mapper = new HostedServicePropertiesResponseMapper();
            HostedService populatedService = mapper.MapToEntity(testResponse);

            Assert.AreEqual(2, populatedService.Deployments.Count());
        }



    }
}
